package com.habdqn.attendance.web.admin;

import com.alibaba.fastjson.JSON;
import com.habdqn.attendance.dto.LayUIData;
import com.habdqn.attendance.entity.Department;
import com.habdqn.attendance.service.DepartmentService;
import com.habdqn.attendance.service.impl.DepartmentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author XingJiejian
 * @date 2019/10/24 0024
 */
@WebServlet(name = "DeptListServlet", urlPatterns = "/admin/dept/list")
public class DeptListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取分页参数
        String strPageNo = request.getParameter("page");
        String strLimit = request.getParameter("limit");
        int pageNo = 1;
        if (strPageNo != null && !"".equals(strPageNo)) {
            pageNo = Integer.parseInt(strPageNo);
        }
        int limit = 10;
        if (strLimit != null && !"".equals(strLimit)) {
            limit = Integer.parseInt(strLimit);
        }
        String likeName = request.getParameter("likeName");

        // 查询分页数据
        DepartmentService service = new DepartmentServiceImpl();
        List<Department> departments = service.queryAllByPage(likeName, pageNo, limit);
        int count = service.getAllCount(likeName);

        //组装响应对象
        LayUIData data = new LayUIData(0, "", count, departments);

        // 响应数据
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(JSON.toJSONString(data));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/admin/dept/list.jsp").forward(request, response);
    }


}
